<template>
  <PageContainer>
    <Header title="我的单位" />
    <view class="container">
      <template v-for="(v, i) in unitList" :key="v.id">
        <view class="unit-item">
          <UnitItem
            :item="v"
            :index="i"
            :is-show-set-default="!userInfo.attestationCompanyIds.length"
            :company-id="getDefaultCompanyId"
            :is-edit-business-card="
              v.id === cocInfo.cocId &&
              (cocInfo.memberRoleType === MemberRoleEnum.PRINCIPAL ||
                cocInfo.memberRoleType === MemberRoleEnum.MANAGER)
            "
            @change-default="changeDefault"
            @refresh="getUnitList"
          />
        </view>
      </template>
      <view class="item-add" @click="toadd">
        <text>您可以添加多个关联单位拓展您的业务</text>
        <view class="add-btn">
          <text class="ali-icon">&#xe61f;</text>
          <text>添加单位</text>
        </view>
      </view>
    </view>
  </PageContainer>
</template>

<script lang="ts" setup>
  /* myUnit 我的单位 */
  import PageContainer from '@/components/PageContainer/index.vue';
  import Header from '@/components/Header/index.vue';
  import { navigateTo, onLoad } from '@/router';
  import { computed, ref, unref } from 'vue';
  import { setDefaultIdentity } from '@/api/mine';
  import { useUserStoreWithOut } from '@/store/modules/user';
  import { storeToRefs } from 'pinia';
  import UnitItem from '../components/UnitItem.vue';
  import { onPullDownRefresh } from '@dcloudio/uni-app';
  import { MemberRoleEnum } from '@/enums/memberRoleEnum';

  const useUser = useUserStoreWithOut();
  const { userInfo, unitList, cocInfo } = storeToRefs(useUser);

  const defaultCompanyId = ref();

  const getDefaultCompanyId = computed(() => unref(defaultCompanyId) || unref(userInfo).companyId);

  const getUnitList = async () => {
    await useUser.setUnitList();
    uni.stopPullDownRefresh();
  };

  onLoad<'myUnit'>(() => {
    getUnitList();
  });

  function changeDefault(bol: boolean) {
    defaultCompanyId.value = bol;
    useUser.setUserInfo();
  }

  function toadd() {
    navigateTo({
      name: 'editAffiliatedCompanies',
      options: {
        success: async () => {
          const isEmpty = !unitList.value.length;
          await getUnitList();
          if (isEmpty && unref(unitList).length) {
            const { id, name, position } = unref(unitList)[0];
            setDefaultIdentity({ companyId: id, companyName: name, companyPositionName: position });
            useUser.setUserInfo();
          }
        },
      },
    });
  }

  onPullDownRefresh(getUnitList);
</script>

<style lang="scss" scoped>
  .container {
    padding: 32rpx;
    padding-top: 0;
    background-color: #fff;
    border-top: 1px solid transparent;
    .unit-item {
      background-color: #f2f3f5;
      padding: 0 32rpx;
      border-radius: 16rpx;
      margin-top: 32rpx;
    }
    .item-add {
      height: 192rpx;
      border-radius: 32rpx;
      margin-top: 32rpx;
      border-radius: 16rpx;
      border: 1px dashed #e0e0dc;
      background: #f7f8fa;
      color: #999996;
      font-size: 28rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      .add-btn {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 48rpx;
        border: 1px solid #2878ff;
        padding: 0 16rpx;
        color: #2878ff;
        font-size: 24rpx;
        border-radius: 24rpx;
        margin-top: 24rpx;
        .ali-icon {
          font-size: 18rpx;
          margin-right: 8rpx;
        }
      }
    }
  }
</style>
